package brave;

public class WallTeam
{
	private WallPlayer[] players;

	public WallTeam()
	{
		this.players = new WallPlayer[3];
	}

	/**
	 * 
	 * @param wallPlayer
	 * @return the slot number the player was added into
	 */
	public Integer addPlayer(WallPlayer wallPlayer)
	{		
		Integer firstOpenSlot;
		if ((firstOpenSlot = this.getFirstOpenSlot()) == null)
		{
			return null;
		}

		players[firstOpenSlot] = wallPlayer;		
		return firstOpenSlot;
	}

	public void dispTeamChat(String chat)
	{
		for (int i = 0; i < 3; i++)
		{
			if (players[i] != null)
			{
				players[i].getBukkitPlayer().sendMessage(chat);
			}
		}		
	}

	public int getNumPlayers()
	{
		int num = 0;
		for (int i = 0; i < 3; i++)
		{
			if (this.players[i] != null)
			{
				num++;
			}
		}
		return num;
	}

	// TODO: put these next two methods in WallWorld
	public void dispAddPlayer(String name)
	{
		this.dispTeamChat(name + " has been added to your team");
	}
	
	public void dispTime(int timeLeft)
	{
		int timeMin = timeLeft / 60;
		int timeSec = timeLeft % 60;
		this.dispTeamChat("TIME " + timeMin + ((timeSec < 10) ? ":0" : ":") + timeSec);
	}

	public WallPlayer getPlayer(int i)
	{
		return this.players[i];
	}
	
	public void removePlayer(String name)
	{
		for (int i = 0; i < 3; i++)
		{
			if (this.players[i] != null && this.players[i].getName().equals(name))
			{
				this.players[i] = null;
			}
		}
	}

	public Integer getFirstOpenSlot()
	{
		for (int i = 0; i < 3; i++)
		{
			if (this.players[i] == null)
			{
				return i;
			}
		}
		return null;
	}
}
